package com.chao.yyks.modular.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.StrUtile;
import com.chao.yyks.modular.sys.bean.EnStudent;
import com.chao.yyks.modular.sys.bean.EnTeacher;
import com.chao.yyks.modular.sys.bean.LoginUser;
import com.chao.yyks.modular.sys.service.EnStudentService;
import com.chao.yyks.modular.sys.service.EnTeacherService;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.VerifyCodeUtils;
import com.chao.common.util.log.Inter;
import com.chao.common.util.reaction.Reaction;
import com.chao.common.util.reaction.ReactionImpl;
import com.chao.common.util.redis.SysRedis;
import com.chao.yyks.util.Digest;
import com.chao.yyks.util.webSocket.WebTtsWs2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import static com.chao.common.util.SysConstant.TOKEN;


/**
 * 系统操作
 * chao
 * 2020-07-18
 */
@RestController
@RequestMapping("/")
public class EnSysController {

	/**
	 * 学生Service
	 */
	@Autowired
	private EnStudentService enStudentService;

	/**
	 * 教师Service
	 */
	@Autowired
	private EnTeacherService enTeacherService;


	/**
	 * 学生登录
	 */
	@Inter(value = "LOGIN_01", grade = "1")
	@RequestMapping(method = RequestMethod.POST, value = "studentLogin")
	public LoginUser login(@RequestBody EnStudent student) {
		return enStudentService.studentLogin(student);
	}

	/**
	 * 小程序登录(学生)
	 */
	@Inter(value = "LOGIN_03", grade = "1")
	@RequestMapping(method = RequestMethod.POST, value = "wxLogin")
	public LoginUser wxLogin(@RequestBody JSONObject data) {
		return enStudentService.wxLogin(data.getString("code"));
	}

	/**
	 * 教师登录
	 */
	@Inter(value = "LOGIN_02", grade = "1")
	@RequestMapping(method = RequestMethod.POST, value = "teacherLogin")
	public LoginUser wxLogin(@RequestBody EnTeacher teacher) {
		return enTeacherService.teacherLogin(teacher);
	}

	/**
	 * 退出登录
	 */
	@Inter(value = "LOGIN_03")
	@RequestMapping(method = RequestMethod.POST, value = "loginOut")
	public Reaction loginOut() {
		SysRedis.delete(TOKEN+":"+RequestUtile.getLoginToken());
		return new ReactionImpl();
	}

	/**
	 * 退出登录
	 */
	@Inter(value = "LOGIN_05")
	@RequestMapping(method = RequestMethod.POST, value = "wxLoginOut")
	public Reaction wxLoginOut() {
		enStudentService.updateOpenId(RequestUtile.getLoginUserId());
		SysRedis.delete(TOKEN+":"+RequestUtile.getLoginToken());
		return new ReactionImpl();
	}

	/**
	 * 获取驰声code
	 */
	@Inter(value = "SYS_SEL_03")
	@RequestMapping(method = RequestMethod.GET, value = "getVerificationJson")
	public JSONObject getVerificationJson() {
		return Digest.getVerificationJson();
	}

	/**
	 * 获取图片验证码
	 * @param response
	 * @throws IOException
	 */
	@Inter(value = "SYS_SEL_01", grade = "1", answer = false)
	@RequestMapping(method = RequestMethod.GET, value = "getVerifyCode")
	public void getVerifyCodeImage(HttpServletResponse response) throws IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 生成图片
		int w = 200, h = 80;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
		//将验证码缓存到redis
		SysRedis.setValue(RequestUtile.getIp(), JSONObject.parseObject("{\"verifyCode\":\""+verifyCode+"\"}"));
		//设置验证码有效期为 300秒(5分钟)
		SysRedis.setTime(RequestUtile.getIp(),300);
	}
}
